using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using CYZHAPI.Common.DB;
using CYZHAPI.Entity;
using CYZHAPI.IRepository;
using Oracle.ManagedDataAccess.Client;

namespace CYZHAPI.Repository {
    public class AscRep : ReponsitoryBase<AscModel>, IAscRep {
        public AscRep (IDapperContext dbContext) : base (dbContext) { }

        public bool delete (long key) {
            return base.delete (key, "id", "ASC_DATA_DIRECTORIES");
        }

        public List<AscModel> GetList (string sWhere) {
            List<AscModel> reList = new List<AscModel> ();
            DataSet ds = base.GetList ("ASC_DATA_DIRECTORIES", sWhere);
            if (ds.Tables[0].Rows.Count > 0) {
                foreach (DataRow dr in ds.Tables[0].Rows) {
                    AscModel objBean = new AscModel () {
                        id = Convert.ToInt32 (dr["id"]),
                        scope = dr["scope"].ToString (),
                        dataformat = dr["dataformat"].ToString (),
                        //year = dr["year"].ToString (),
                        //mouth = dr["mouth"].ToString (),
                        //day = dr["day"].ToString (),
                        filename = dr["filename"].ToString (),
                        createtime = Convert.ToDateTime (dr["createtime"]),
                        createby = dr["createby"].ToString (),
                        updatetime = Convert.ToDateTime (dr["updatetime"]),
                        updateby = dr["updateby"].ToString (),
                        filedatatime = Convert.ToDateTime (dr["filedatatime"]).Date
                    };
                    reList.Add (objBean);
                    //foreach (DataColumn dc in ds.Tables[0].Columns) {}
                }
            } else {
                return null;
            }

            return reList;
        }

        public bool insert (string TableName, List<AscModel> itemModel) {
            DataSet DataSetItem = new DataSet ();
            DataTable dt = new DataTable (TableName); //创建一个名为Table的DataTalbe

            dt.Columns.Add (new DataColumn ("id", typeof (int))); //为dt_dry表内建立Column
            dt.Columns.Add (new DataColumn ("dataformat", typeof (string)));
            dt.Columns.Add (new DataColumn ("scope", typeof (string)));
            //dt.Columns.Add (new DataColumn ("year", typeof (string)));
            //dt.Columns.Add (new DataColumn ("mouth", typeof (string)));
            //dt.Columns.Add (new DataColumn ("day", typeof (string)));
            dt.Columns.Add (new DataColumn ("filename", typeof (string)));
            dt.Columns.Add (new DataColumn ("filedatatime", typeof (DateTime)));
            dt.Columns.Add (new DataColumn ("createtime", typeof (DateTime)));
            dt.Columns.Add (new DataColumn ("createby", typeof (string)));
            dt.Columns.Add (new DataColumn ("updatetime", typeof (DateTime)));
            dt.Columns.Add (new DataColumn ("updateby", typeof (string)));

            foreach (AscModel v in itemModel) {
                DataRow dr = dt.NewRow (); //注意这边创建dt的新行的方法。指定类型是DataRow而不是TableRow，然后不用new直接的用创建的DataTable下面的NewRow方法。
                dr["id"] = v.id;//this.GetProblemXH ("ASC_DATA_SEQUENCE");
                dr["dataformat"] = v.dataformat;
                dr["scope"] = v.scope;
                //dr["year"] = v.year;
                //dr["mouth"] = v.mouth;
                //dr["day"] = v.day;
                dr["filename"] = v.filename;
                dr["filedatatime"] = v.filedatatime;
                dr["createtime"] =v.createtime; //DateTime.Now.Date;
                dr["createby"] = v.createby;
                dr["updatetime"] = v.updatetime;//DateTime.Now.Date;
                dr["updateby"] = v.updateby;
                dt.Rows.Add (dr);
            }
            DataSetItem.Tables.Add (dt);
            return base.insert (TableName, DataSetItem);
        }

        public List<AscModel> Seach () {
            throw new System.NotImplementedException ();
        }

        public bool update (long key, AscModel itemModel) { //ASC_DATA_DIRECTORIES
            DataSet DataSetItem = new DataSet ();
            DataTable dt = new DataTable ("ASC_DATA_DIRECTORIES"); //创建一个名为Table的DataTalbe

            dt.Columns.Add (new DataColumn ("id", typeof (Decimal))); //为dt_dry表内建立Column
            dt.Columns.Add (new DataColumn ("dataformat", typeof (string)));
            dt.Columns.Add (new DataColumn ("scope", typeof (string)));
            //dt.Columns.Add (new DataColumn ("year", typeof (string)));
            //dt.Columns.Add (new DataColumn ("mouth", typeof (string)));
            //dt.Columns.Add (new DataColumn ("day", typeof (string)));
            dt.Columns.Add (new DataColumn ("filename", typeof (string)));
            dt.Columns.Add (new DataColumn ("filedatatime", typeof (DateTime)));
            dt.Columns.Add (new DataColumn ("updatetime", typeof (DateTime)));
            dt.Columns.Add (new DataColumn ("updateby", typeof (string)));

            DataRow dr = dt.NewRow (); //注意这边创建dt的新行的方法。指定类型是DataRow而不是TableRow，然后不用new直接的用创建的DataTable下面的NewRow方法。
            dr["id"] =key;
            dr["dataformat"] = itemModel.dataformat;
            dr["scope"] = itemModel.scope;
            //dr["year"] = itemModel.year;
            //dr["mouth"] = itemModel.mouth;
            //dr["day"] = itemModel.day;
            dr["filename"] = itemModel.filename;
            dr["filedatatime"] = itemModel.filedatatime;
            dr["updatetime"] = DateTime.Now.Date;
            dr["updateby"] = itemModel.updateby;
            dt.Rows.Add (dr);
            DataSetItem.Tables.Add (dt);
            return base.update ("id", "ASC_DATA_DIRECTORIES", DataSetItem);
        }
        public decimal degetNextSecquece(string secName){
            return this.GetProblemXH (secName);
        }
    }
}